const express = require('express')
const riskDao = require('../database/riskDao')
const router = express.Router()

// 根据农田id获取农田信息与建议
router.get('/getSuggest',async (req,res) => {
  let farmlandId = req.query.farmlandId
  try {
    let result = await riskDao.getSuggest(farmlandId)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    console.log(err);
    res.send({
      code:500,
      msg:'获取错误',
      data:err
    });
  }
})

// 根据风险等级来获取农田信息与建议 只获取高风险等级的建议
router.get('/getRiskSuggest',async (req,res) => {
  try {
    let result = await riskDao.getRiskSuggest()
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    console.log(err);
    res.send({
      code:500,
      msg:'获取错误',
      data:err
    });
  }
})

// 根据农田id修改农田建议完成状态
router.post('/modifyState',async (req,res) => {
  // 传入的是新状态
  let {farmlandId,newState} = req.body
  // 这里最好加一个判断，判断有没有该农田
  try{
    let result = await riskDao.modifyState(farmlandId,newState)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    console.log(err);
    res.send({
      code:500,
      msg:'获取错误',
      data:err
    });
  }
})

module.exports = router